package com.cqjtu.store.system.controller;

import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.R;
import com.cqjtu.store.system.service.ProductStatsService;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * ClassName: ProductStatsController
 * Package: com.cqjtu.store.system.controller
 * Description:
 *
 * @Author LIEX
 * @create 2024/3/11 11:22
 * @Version 1.0
 */
@RestController
@RequestMapping("product/stats")
public class ProductStatsController {
    @Resource
    ProductStatsService productStatsService;
    @PutMapping("/{productId}")
    public R<Void> updateProductStats(HttpServletRequest request,
                @PathVariable("productId") Integer productId){
        String token = request.getHeader("Authorization");
        if(token!=null&&!token.isEmpty()){
            UserLoginVO userLoginVO = JwtUtils.parseToken(token);
            productStatsService.updateProductStats(userLoginVO.getId(),productId);
        }

        return null;
    }
    @PutMapping
    public R<Void> updateStats(HttpServletRequest request){

        //获取用户的身份
        String token = request.getHeader("Authorization");
        if(token!=null&&!token.isEmpty()){
            UserLoginVO userLoginVO = JwtUtils.parseToken(token);
            productStatsService.updateStats(userLoginVO.getId());
        }
        return R.ok();
    }
}
